package com.aco.fatureapp.activity;

import java.util.Iterator;

import roboguice.inject.InjectView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.aco.fatureapp.R;
import com.aco.fatureapp.model.ConfiguracaoApp;
import com.aco.fatureapp.utilitary.Utils;
import com.mobsandgeeks.saripaar.annotation.Required;

public class ConfigurationAppActivity extends BaseActivity {

	@InjectView(R.id.ipConexao)
	@Required(order = 1, messageResId = R.string.lblIpConexaoRequerid)
	private EditText mTxtIp;

	@InjectView(R.id.portConexao)
	@Required(order = 1, messageResId = R.string.lblPortaDeConecaoRequerid)
	private EditText mTxtPorta;

	@InjectView(R.id.nomeServicoAutenticacao)
	@Required(order = 1, messageResId = R.string.lblNomeServicoWebAutRequerid)
	private EditText mTxtServicoAutentica;

	@InjectView(R.id.nomeServicoApp)
	@Required(order = 1, messageResId = R.string.lblNomeServicoWebRequerid)
	private EditText mTxtServicoApp;

	@InjectView(R.id.qtdLeituraEnvio)
	@Required(order = 1, messageResId = R.string.lblQtdLeituraEnvioRequerid)
	private EditText mTxtQtdeEnvio;

	@InjectView(R.id.qtdEmitirResumo)
	@Required(order = 1, messageResId = R.string.lblQtdEmitirResumoRequerid)
	private EditText mTxtQtdeResumo;

	@InjectView(R.id.qtdTempoEnvio)
	@Required(order = 1, messageResId = R.string.lblQtdTempoEnvioRequerid)
	private EditText mTxtTempoEnvio;

	private ConfiguracaoApp mEntity;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.aco.fatureapp.activity.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_configuration_app);

		findConfiguration();
	}

	/**
	 * Verificar se já existe uma configuração gravada no banco e mostrar os
	 * dados na tela.
	 */
	private void findConfiguration() {
		final Iterator<ConfiguracaoApp> lstConfiguracao = ConfiguracaoApp
				.findAll(ConfiguracaoApp.class);

		if (lstConfiguracao != null && lstConfiguracao.hasNext()) {
			mEntity = lstConfiguracao.next();

			// Popular a tela com os campos do banco.
			mTxtIp.setText(mEntity.getIpConexao());
			mTxtPorta.setText(mEntity.getPortaDeConecao());
			mTxtServicoApp.setText(mEntity.getNomeServicoWeb());
			mTxtQtdeEnvio.setText(mEntity.getQtdeLeituraEnvio().toString());
			mTxtQtdeResumo.setText(mEntity.getQtdeEmitirResumo().toString());
			mTxtTempoEnvio.setText(mEntity.getTempoEnvio().toString());
			mTxtServicoAutentica.setText(mEntity
					.getNomeServicoWebAutenticacao());
		}
		else {
			mTxtIp.setText("192.168.25.25");
			mTxtPorta.setText("8080");
			mTxtServicoApp.setText("ifature/rest/importBean");
			mTxtQtdeEnvio.setText("50");
			mTxtQtdeResumo.setText("50");
			mTxtTempoEnvio.setText("50");
			mTxtServicoAutentica.setText("ifature/rest/autenticationBean");
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.action_bar_menu_config_app, menu);

		getActionBar().setDisplayHomeAsUpEnabled(true);

		return true;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		case R.id.menu_salvar: {
			validator.validate();
			return true;
		}
		case R.id.menu_cancelar: {
			finish();
			return true;
		}
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.aco.fatureapp.activity.BaseActivity#onValidationSucceeded()
	 */
	@Override
	public void onValidationSucceeded() {

		// Instânciar o objeto para persist.
		if (mEntity == null || mEntity.getId() == null) {
			mEntity = new ConfiguracaoApp();
		}

		// Popular os campos com os valores que estão em tela.
		mEntity.setIpConexao(mTxtIp.getText().toString());

		mEntity.setPortaDeConecao(mTxtPorta.getText().toString());

		mEntity.setNomeServicoWebAutenticacao(mTxtServicoAutentica.getText()
				.toString());

		mEntity.setNomeServicoWeb(mTxtServicoApp.getText().toString());

		mEntity.setQtdeLeituraEnvio(Integer.valueOf(mTxtQtdeEnvio.getText()
				.toString()));

		mEntity.setQtdeEmitirResumo(Integer.valueOf(mTxtQtdeResumo.getText()
				.toString()));

		mEntity.setTempoEnvio(Integer.valueOf(mTxtTempoEnvio.getText()
				.toString()));

		mEntity.save();

		Utils.alert(ConfigurationAppActivity.this,
				"Configuração salva com sucesso!", null);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
